package com.teamwork.module03.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teamwork.common.entity.common.User;
import com.teamwork.common.entity.module03.*;
import com.teamwork.common.result.Result;
import com.teamwork.module03.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class DataServiceImpl implements DataService {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleLikeService articleLikeService;
    @Resource
    private CommentService commentService;
    @Resource
    private ArticleCaresService articleCaresService;
    @Resource
    private UserService userService;
    @Resource
    private ReplyService replyService;

    /**
     * 获取文章统计数据分析
     * @param userid 用户id
     * @return 分析结果
     */
    @Override
    public Result getArticleDatas(Integer userid) {

        //先获取该用户发布过得文章的id集合
        List<Article> articles = articleService.getArtArdyPubByUId(userid);

        List<Long> aids = articles.stream().map(Article::getId).collect(Collectors.toList());

        LambdaQueryWrapper<ArticleLikes> likeWrapper =new LambdaQueryWrapper<>();

        likeWrapper.in(ArticleLikes::getArticleId,aids);

        List<ArticleLikes> likes = articleLikeService.list(likeWrapper);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowday = format.format(new Date());

//      //初始化一个计数器记录今日点赞量
        AtomicInteger todayLikes = new AtomicInteger();
        likes.forEach((it)->{
            if( it.getCreatetime().toLocalDate().toString().equals(nowday))
                todayLikes.getAndIncrement();
        });
       //在查询总点赞量
        long totalLikes = articles.stream().map(Article::getLikes).count();
        //再查询今日评论数量
        LambdaQueryWrapper<Comment> cwper = new LambdaQueryWrapper<>();
        cwper.in(Comment::getArticleId,aids);
        List<Comment> comments = commentService.list(cwper);
        AtomicInteger todayComments = new AtomicInteger();
        comments.forEach((it)->{
            if( it.getCreatetime().toLocalDate().toString().equals(nowday))
                todayComments.getAndIncrement();
        });
        //再查询总评论数量
        long totalComments = articles.stream().map(Article::getComments).count();

        //再查询今日收藏量
        LambdaQueryWrapper<ArticleCares> caresWrapper =new LambdaQueryWrapper<>();
        caresWrapper.in(ArticleCares::getArticleId,aids);
        List<ArticleCares> articleCares = articleCaresService.list(caresWrapper);
        AtomicInteger todayCares = new AtomicInteger();
        articleCares.forEach((it)->{
            if(it.getCreatetime().toLocalDate().toString().equals(nowday));
            todayCares.getAndIncrement();
        });
        //再查询总收藏量
        long totalCares = articles.stream().map(Article::getShares).count();

        HashMap<String, Object> map = new HashMap<>();
        map.put("todayLikes",todayLikes);
        map.put("todayComments",todayComments);
        map.put("todayCares",todayCares);
        map.put("totalLikes",totalLikes);
        map.put("totalComments",totalComments);
        map.put("totalCares",totalCares);

        return Result.success(map);
    }

    /**
     * 获取用户发布的最火的前五个文章(根据点赞，评论，收藏来判断)
     * @param userid 用户id
     * @return
     */
    @Override
    public Result getHotArticles(Integer userid) {
        //先获取该用户发布过得文章的id集合
        List<Article> articles = articleService.getArtArdyPubByUId(userid);

        List<Long> aids = articles.stream().map(Article::getId).collect(Collectors.toList());


        //先根据点赞查询
        LambdaQueryWrapper<Article> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Article::getId,aids).orderByDesc(Article::getLikes).or().orderByDesc(Article::getShares).
                or().orderByDesc(Article::getComments);

        Page<Article> page = articleService.page(new Page<>(1, 5), wrapper);


        return Result.success(page);
    }

    /**
     * 管理员获取平台数据
     * @param userid 用户id
     * @return 数据
     */
    @Override
    public Result getDatas(Integer userid) {
       //先判断是否为管理员
        Integer role = userService.getById(userid).getRole();
        if(role!=3){
            return  Result.failure("不是管理员，无权查看");
        }
        //先统计今日注册用户

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowday = format.format(new Date());
        List<User> userList = userService.list();

        AtomicInteger todayUsers =new AtomicInteger();
        userList.forEach(it->{
            if (it.getCreatetime().toLocalDate().toString().equals(nowday)) {
                todayUsers.getAndIncrement();
            }
        });
        //获取总注册人数
        long totalUsers = userService.count();
        //再查询今天发布的所有文章
        List<Article> articleList = articleService.list();
        AtomicInteger todayArticles =new AtomicInteger();
        articleList.forEach(it->{
            if (it.getCreatetime().toLocalDate().toString().equals(nowday)) {
                todayArticles.getAndIncrement();
            }
        });
        //获取总发布文章数
        long totalArticles = articleService.count();
        //获取今日评论和回复数
        List<Comment> commentList = commentService.list();
        List<Reply> replyList = replyService.list();

        AtomicInteger todayComments = new AtomicInteger();
        commentList.forEach(it->{
            if (it.getCreatetime().toLocalDate().toString().equals(nowday)) {
                todayComments.getAndIncrement();
            }
        });
        replyList.forEach(it->{
            if (it.getCreatetime().toLocalDate().toString().equals(nowday)) {
                todayComments.getAndIncrement();
            }
        });
        //获取总评论和回复数的和
        long ccout = commentService.count();
        long rcount = replyService.count();
        long totalComments = ccout+rcount;

        HashMap<String, Object> map = new HashMap<>();
        map.put("todayUsers",todayUsers);
        map.put("todayArticles",todayArticles);
        map.put("todayComments",todayComments);
        map.put("totalUsers",totalUsers);
        map.put("totalArticles",totalArticles);
        map.put("totalComments",totalComments);

        return Result.success(map);


    }
}
